<?php
class Pay_record_model extends CI_Model
{
    private $table = 'pay_record';
    
    public function updateBitStateByPayId($pay_id)
    {
        $data = array(
            'bit_state' => '65536'
        );
        $this->db->where('pay_id',$pay_id);
        return $this->db->update($this->table,$data);
    }
    
    public function findByPayId($payId)
    {
        $this->db->where('pay_id', $payId);
        return $this->db->get($this->table);
    }
    
    public function insertPayRecord($params=array())
    {
        $data = array(
            'level'      => isset($params['level']) ? $params['level'] : 0,
            'cat_ids'    => isset($params['cat_ids']) ? $params['cat_ids'] : 0,
            'goods_ids'  => isset($params['goods_ids']) ? $params['goods_ids'] : 0,
            'goods_nums' => isset($params['goods_nums']) ? $params['goods_nums'] : 0,
            'uid'        => $params['uid'],
            'phone'      => $params['phone'],
            'pay_method' => isset($params['pay_method']) ? $params['pay_method'] : 0,
            'order_bouns'=> isset($params['order_bouns']) ? $params['order_bouns'] : 0,
            'amount'     => isset($params['amount']) ? $params['amount'] : 0,
            'subject'    => $params['subject'],
            'body'       => $params['body'],
            'info'       => isset($params['info']) ? $params['info'] : 0,
            'bit_state'  => isset($params['bit_state']) ? $params['bit_state'] : 0,
            'apply_time' => time(),
            'pay_time'   => isset($params['pay_time']) ? $params['pay_time'] : 0,
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    public function updatePayRecord($params=array())
    {
        if (isset($params['level'])) {
            $data['level'] = $params['level'];
        }
        if (isset($params['cat_ids'])) {
            $data['cat_ids'] = $params['cat_ids'];
        }
        if (isset($params['goods_ids'])) {
            $data['goods_ids'] = $params['goods_ids'];
        }
        if (isset($params['goods_nums'])) {
            $data['goods_nums'] = $params['goods_nums'];
        }
        if (isset($params['order_ids'])) {
            $data['order_ids'] = $params['order_ids'];
        }
        if (isset($params['pay_method'])) {
            $data['pay_method'] = $params['pay_method'];
        }
        if (isset($params['order_bouns'])) {
            $data['order_bouns'] = $params['order_bouns'];
        }
        if (isset($params['amount'])) {
            $data['amount'] = $params['amount'];
        }
        if (!empty($params['body'])) {
            $data['body'] = $params['body'];
        }
        if (!empty($params['bit_state'])) {
            $this->db->set('bit_state', 'bit_state+'.$params['bit_state'], FALSE);
        }
        if (!empty($params['pay_time'])) {
            $data['pay_time'] = $params['pay_time'];
        }
        if (!empty($params['serial_number'])) {
            $data['serial_number'] = $params['serial_number'];
        }
        if (!empty($params['uid'])) {
            $this->db->where('uid', $params['uid']);
        }
        $this->db->where('pay_id', $params['pay_id']);
        return $this->db->update($this->table, $data);
    }
}